﻿Option Explicit On ' Make sure all items are explicitly declared
Option Strict On ' Be Very Strict about things here!
Namespace ZuneCardr
    ''' <summary>Settings</summary>
    ''' <author>Comentsys</author>
    ''' <version>2.0.0</version>
    ''' <created>22nd February 2009</created>
    ''' <modified>23rd February 2009</modified>
    ''' <remarks>Settings</remarks>
    Public Class Settings

        ' Private Constants

        Private Const XML_VERSION As String = "1.0"
        Private Const XML_ENCODE As String = "utf-8"
        Private Const XML_YES As String = "yes"
        ' Tags
        Private Const TAG_ROOT As String = "zunecardr"
        Private Const TAG_SETTINGS As String = "settings"
        Private Const TAG_STYLE As String = "style"
        Private Const TAG_DISPLAY As String = "display"
        Private Const TAG_ZUNE As String = "zune"
        Private Const TAG_THEME As String = "theme"
        ' Attributes
        Private Const ATTR_VERSION As String = "version"
        Private Const ATTR_TYPE As String = "type"
        Private Const ATTR_CUSTOM As String = "custom"
        Private Const ATTR_AVATAR As String = "avatar"
        Private Const ATTR_BACKGROUND As String = "bg"
        Private Const ATTR_COVERS As String = "covers"
        Private Const ATTR_COVER_TEXT As String = "covertext"
        Private Const ATTR_PLAYS As String = "plays"
        Private Const ATTR_BADGES As String = "badges"
        Private Const ATTR_STATUS As String = "status"
        Private Const ATTR_PLAYED As String = "played"
        Private Const ATTR_WEBLINK As String = "link"
        Private Const ATTR_ZUNE As String = "zune"
        Private Const ATTR_COLOUR As String = "colour"
        Private Const ATTR_GRADIENT_START As String = "start"
        Private Const ATTR_GRADIENT_END As String = "end"
        Private Const ATTR_FOREGROUND As String = "fg"
        Private Const ATTR_BORDER As String = "border"
        Private Const ATTR_SECTION_FOREGROUND As String = "sectionfg"
        Private Const ATTR_SECTION_BACKGROUND As String = "sectionbg"
        Private Const ATTR_ITEM_FOREGROUND As String = "itemfg"
        ' Values
        Private Const VAL_VERSION As String = "2.0"

        ' Private Members

        Private _Style As Integer = 0
        Private _Custom As String = ""
        Private _ShowAvatar As Boolean
        Private _ShowBackground As Boolean
        Private _ShowCovers As Boolean
        Private _ShowCoverText As Boolean
        Private _ShowPlays As Boolean
        Private _ShowBadges As Boolean
        Private _ShowStatus As Boolean
        Private _ShowLastPlayed As Boolean
        Private _ShowWeblink As Boolean
        Private _ShowZune As Boolean
        Private _Zune As String = ""
        Private _ZuneColour As String = "#FF00FF00"
        Private _Theme As Integer = 0
        Private _GradientStart As String = "#FF00FF00"
        Private _GradientEnd As String = "#FF00FF00"
        Private _Foreground As String = "#FF00FF00"
        Private _Background As String = "#FF00FF00"
        Private _Border As String = "#FF00FF00"
        Private _SectionForeground As String = "#FF00FF00"
        Private _SectionBackground As String = "#FF00FF00"
        Private _ItemForeground As String = "#FF00FF00"

        ' Public Methods

        ''' <summary>SerialiseColour</summary>
        ''' <param name="Colour">Source Color</param>
        ''' <returns>Colour String (#FF00FF00)</returns>
        ''' <remarks>Converts Colour to String</remarks>
        Public Shared Function SerialiseColour(ByVal Colour As Color) As String
            Return String.Format("{0:x8}", Colour.ToString)
        End Function

        ''' <summary>DeserialiseColour</summary>
        ''' <param name="Colour">Source String</param>
        ''' <returns>Colour Value</returns>
        ''' <remarks>Converts String to Colour</remarks>
        Public Shared Function DeserialiseColour(ByVal Colour As String) As Color
            Return CType(ColorConverter.ConvertFromString(Trim(Colour)), Color)
        End Function

        ' Private Methods

        ''' <summary>DeserialiseBoolean</summary>
        ''' <param name="Value">Boolean string</param>
        ''' <returns>Boolean Value</returns>
        ''' <remarks>Converts String to Boolean</remarks>
        Private Function DeserialiseBoolean(ByVal Value As String) As Boolean
            Return LCase(Trim(Value)).Equals("true") ' If True, False Otherwise
        End Function

        ''' <summary>GetStyle</summary>
        ''' <returns>XElement</returns>
        ''' <remarks>Gets Style XML Section</remarks>
        Private Function GetStyle() As XElement
            Return New XElement(TAG_STYLE, New XAttribute(ATTR_TYPE, _Style), New XAttribute(ATTR_CUSTOM, Custom))
        End Function

        ''' <summary>SetStyle</summary>
        ''' <param name="Section">XElement Section</param>
        ''' <remarks>Sets Style Information from XML</remarks>
        Private Sub SetStyle(ByRef Section As XElement)
            _Style = CInt(Section.Attribute(ATTR_TYPE).Value())
            _Custom = Trim(Section.Attribute(ATTR_CUSTOM).Value())
        End Sub

        ''' <summary>GetDisplay</summary>
        ''' <returns>XElement</returns>
        ''' <remarks>Gets Display XML Section</remarks>
        Private Function GetDisplay() As XElement
            Return New XElement(TAG_DISPLAY, New XAttribute(ATTR_AVATAR, ShowAvatar), _
                                New XAttribute(ATTR_BACKGROUND, ShowBackground), _
                                New XAttribute(ATTR_COVERS, ShowCovers), _
                                New XAttribute(ATTR_COVER_TEXT, ShowCoverText), _
                                New XAttribute(ATTR_PLAYS, ShowPlays), _
                                New XAttribute(ATTR_BADGES, ShowBadges), _
                                New XAttribute(ATTR_STATUS, ShowStatus), _
                                New XAttribute(ATTR_PLAYED, ShowLastPlayed), _
                                New XAttribute(ATTR_WEBLINK, ShowWeblink), _
                                New XAttribute(ATTR_ZUNE, ShowZune))
        End Function

        ''' <summary>SetDisplay</summary>
        ''' <param name="Section">XElement Section</param>
        ''' <remarks>Sets Display Information from XML</remarks>
        Private Sub SetDisplay(ByRef Section As XElement)
            _ShowAvatar = DeserialiseBoolean(Section.Attribute(ATTR_AVATAR).Value())
            _ShowBackground = DeserialiseBoolean(Section.Attribute(ATTR_BACKGROUND).Value())
            _ShowCovers = DeserialiseBoolean(Section.Attribute(ATTR_COVERS).Value())
            _ShowCoverText = DeserialiseBoolean(Section.Attribute(ATTR_COVER_TEXT).Value())
            _ShowPlays = DeserialiseBoolean(Section.Attribute(ATTR_PLAYS).Value())
            _ShowBadges = DeserialiseBoolean(Section.Attribute(ATTR_BADGES).Value())
            _ShowStatus = DeserialiseBoolean(Section.Attribute(ATTR_STATUS).Value())
            _ShowLastPlayed = DeserialiseBoolean(Section.Attribute(ATTR_PLAYED).Value())
            _ShowWeblink = DeserialiseBoolean(Section.Attribute(ATTR_WEBLINK).Value())
            _ShowZune = DeserialiseBoolean(Section.Attribute(ATTR_ZUNE).Value())
        End Sub

        ''' <summary>GetZune</summary>
        ''' <returns>XElement</returns>
        ''' <remarks>Gets Zune XML Section</remarks>
        Private Function GetZune() As XElement
            Return New XElement(TAG_ZUNE, New XAttribute(ATTR_TYPE, _Zune), _
                                New XAttribute(ATTR_COLOUR, _ZuneColour))
        End Function

        ''' <summary>SetZune</summary>
        ''' <param name="Section">XElement Section</param>
        ''' <remarks>Sets Zune Information from XML</remarks>
        Private Sub SetZune(ByRef Section As XElement)
            _Zune = Trim(Section.Attribute(ATTR_TYPE).Value)
            _ZuneColour = Trim(Section.Attribute(ATTR_COLOUR).Value)
        End Sub

        ''' <summary>GetTheme</summary>
        ''' <returns>XElement</returns>
        ''' <remarks>Gets Theme XML Section</remarks>
        Private Function GetTheme() As XElement
            Return New XElement(TAG_THEME, New XAttribute(ATTR_TYPE, _Theme), _
                                New XAttribute(ATTR_GRADIENT_START, _GradientStart), _
                                New XAttribute(ATTR_GRADIENT_END, _GradientEnd), _
                                New XAttribute(ATTR_FOREGROUND, _Foreground), _
                                New XAttribute(ATTR_BACKGROUND, _Background), _
                                New XAttribute(ATTR_BORDER, _Border), _
                                New XAttribute(ATTR_SECTION_FOREGROUND, _SectionForeground), _
                                New XAttribute(ATTR_SECTION_BACKGROUND, _SectionBackground), _
                                New XAttribute(ATTR_ITEM_FOREGROUND, _ItemForeground))
        End Function

        ''' <summary>SetTheme</summary>
        ''' <param name="Section">XElement Section</param>
        ''' <remarks>Sets Theme Information from XML</remarks>
        Private Sub SetTheme(ByRef Section As XElement)
            _Theme = CInt(Section.Attribute(ATTR_TYPE).Value)
            _GradientStart = Section.Attribute(ATTR_GRADIENT_START).Value
            _GradientEnd = Section.Attribute(ATTR_GRADIENT_END).Value
            _Foreground = Section.Attribute(ATTR_FOREGROUND).Value
            _Background = Section.Attribute(ATTR_BACKGROUND).Value
            _Border = Section.Attribute(ATTR_BORDER).Value
            _SectionForeground = Section.Attribute(ATTR_SECTION_FOREGROUND).Value
            _SectionBackground = Section.Attribute(ATTR_SECTION_BACKGROUND).Value
            _ItemForeground = Section.Attribute(ATTR_ITEM_FOREGROUND).Value
        End Sub

        ' Public Methods

        ''' <summary>Save</summary>
        ''' <param name="Filename">Filename</param>
        ''' <remarks>Save ZuneCardr Settings</remarks>
        Public Sub Save(ByVal Filename As String)
            Dim Document As New XDocument(New XDeclaration(XML_VERSION, XML_ENCODE, XML_YES), _
                                          New XElement(TAG_ROOT, New XAttribute(ATTR_VERSION, VAL_VERSION), _
                                                       New XElement(TAG_SETTINGS, GetStyle, _
                                                                    GetDisplay, GetZune, _
                                                                    GetTheme)))
            Document.Save(Filename) ' Save
        End Sub

        ''' <summary>Open</summary>
        ''' <param name="Filename">Filename</param>
        ''' <remarks>Open ZuneCardr Settings</remarks>
        Public Sub Open(ByVal Filename As String)
            Dim Document As XElement
            Document = XElement.Load(Xml.XmlReader.Create(Filename))
            If Document.Name.LocalName = TAG_ROOT Then ' Process Data
                If Trim(Document.Attribute(ATTR_VERSION).Value) = VAL_VERSION Then ' Check Version
                    SetStyle(Document.Element(TAG_SETTINGS).Element(TAG_STYLE)) ' Style
                    SetDisplay(Document.Element(TAG_SETTINGS).Element(TAG_DISPLAY)) ' Display
                    SetZune(Document.Element(TAG_SETTINGS).Element(TAG_ZUNE)) ' Zune
                    SetTheme(Document.Element(TAG_SETTINGS).Element(TAG_THEME)) ' Theme
                End If
            End If
        End Sub

        ' Public Properties

        ''' <summary>Style</summary>
        ''' <value>Card.CardStyle</value>
        ''' <returns>Card Style</returns>
        ''' <remarks>Internal Storage as Integer</remarks>
        Public Property Style() As Card.CardStyle
            Get
                Return CType(_Style, Card.CardStyle)
            End Get
            Set(ByVal Value As Card.CardStyle)
                _Style = Value
            End Set
        End Property

        ''' <summary>Custom</summary>
        ''' <value>String</value>
        ''' <returns>Custom Filename</returns>
        ''' <remarks>Custom Style Filename</remarks>
        Public Property Custom() As String
            Get
                Return _Custom
            End Get
            Set(ByVal Value As String)
                _Custom = Value
            End Set
        End Property

        ''' <summary>ShowAvatar</summary>
        ''' <returns>Show Avatar</returns>
        ''' <remarks>Gets / Sets Show Avatar</remarks>
        Public Property ShowAvatar() As Boolean
            Get
                Return _ShowAvatar
            End Get
            Set(ByVal Value As Boolean)
                _ShowAvatar = Value
            End Set
        End Property

        ''' <summary>ShowBackground</summary>
        ''' <returns>Show Background</returns>
        ''' <remarks>Gets / Sets Show Background</remarks>
        Public Property ShowBackground() As Boolean
            Get
                Return _ShowBackground
            End Get
            Set(ByVal Value As Boolean)
                _ShowBackground = Value
            End Set
        End Property

        ''' <summary>ShowCovers</summary>
        ''' <returns>Show Covers</returns>
        ''' <remarks>Gets / Sets Show Covers</remarks>
        Public Property ShowCovers() As Boolean
            Get
                Return _ShowCovers
            End Get
            Set(ByVal Value As Boolean)
                _ShowCovers = Value
            End Set
        End Property

        ''' <summary>ShowCoverText</summary>
        ''' <returns>Show Cover Text</returns>
        ''' <remarks>Gets / Sets Show Cover Text</remarks>
        Public Property ShowCoverText() As Boolean
            Get
                Return _ShowCoverText
            End Get
            Set(ByVal Value As Boolean)
                _ShowCoverText = Value
            End Set
        End Property

        ''' <summary>ShowPlays</summary>
        ''' <returns>Show Plays</returns>
        ''' <remarks>Gets / Sets Show Plays</remarks>
        Public Property ShowPlays() As Boolean
            Get
                Return _ShowPlays
            End Get
            Set(ByVal Value As Boolean)
                _ShowPlays = Value
            End Set
        End Property

        ''' <summary>ShowBadges</summary>
        ''' <returns>Show Badges</returns>
        ''' <remarks>Gets / Sets Show Badges</remarks>
        Public Property ShowBadges() As Boolean
            Get
                Return _ShowBadges
            End Get
            Set(ByVal Value As Boolean)
                _ShowBadges = Value
            End Set
        End Property

        ''' <summary>ShowStatus</summary>
        ''' <returns>Show Status</returns>
        ''' <remarks>Gets / Sets Show Status</remarks>
        Public Property ShowStatus() As Boolean
            Get
                Return _ShowStatus
            End Get
            Set(ByVal Value As Boolean)
                _ShowStatus = Value
            End Set
        End Property

        ''' <summary>ShowLastPlayed</summary>
        ''' <returns>Show Last Played</returns>
        ''' <remarks>Gets / Sets Show Last Played</remarks>
        Public Property ShowLastPlayed() As Boolean
            Get
                Return _ShowLastPlayed
            End Get
            Set(ByVal Value As Boolean)
                _ShowLastPlayed = Value
            End Set
        End Property

        ''' <summary>ShowWeblink</summary>
        ''' <returns>Show Weblink</returns>
        ''' <remarks>Gets / Sets Show Weblink</remarks>
        Public Property ShowWeblink() As Boolean
            Get
                Return _ShowWeblink
            End Get
            Set(ByVal Value As Boolean)
                _ShowWeblink = Value
            End Set
        End Property

        ''' <summary>ShowZune</summary>
        ''' <returns>Show Zune</returns>
        ''' <remarks>Gets / Sets Show Zune</remarks>
        Public Property ShowZune() As Boolean
            Get
                Return _ShowZune
            End Get
            Set(ByVal Value As Boolean)
                _ShowZune = Value
            End Set
        End Property

        ''' <summary>Zune</summary>
        ''' <returns>Zune</returns>
        ''' <remarks>Gets / Sets Zune</remarks>
        Public Property Zune() As String
            Get
                Return _Zune
            End Get
            Set(ByVal Value As String)
                _Zune = Value
            End Set
        End Property

        ''' <summary>ZuneColour</summary>
        ''' <returns>Zune Colour</returns>
        ''' <remarks>Internal Storage as String</remarks>
        Public Property ZuneColour() As Color
            Get
                Return DeserialiseColour(_ZuneColour)
            End Get
            Set(ByVal Value As Color)
                _ZuneColour = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>Theme</summary>
        ''' <value>Theme.Style</value>
        ''' <returns>Theme Style</returns>
        ''' <remarks>Internal Storage as Integer</remarks>
        Public Property Theme() As Theme.Style
            Get
                Return CType(_Theme, Theme.Style)
            End Get
            Set(ByVal Value As Theme.Style)
                _Theme = Value
            End Set
        End Property

        ''' <summary>GradientStart</summary>
        ''' <value>Color</value>
        ''' <returns>GradientStart Colour</returns>
        Public Property GradientStart() As Color
            Get
                Return DeserialiseColour(_GradientStart)
            End Get
            Set(ByVal Value As Color)
                _GradientStart = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>GradientEnd</summary>
        ''' <value>Color</value>
        ''' <returns>GradientEnd Colour</returns>
        Public Property GradientEnd() As Color
            Get
                Return DeserialiseColour(_GradientEnd)
            End Get
            Set(ByVal Value As Color)
                _GradientEnd = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>Foreground</summary>
        ''' <value>Color</value>
        ''' <returns>Foreground Colour</returns>
        Public Property Foreground() As Color
            Get
                Return DeserialiseColour(_Foreground)
            End Get
            Set(ByVal Value As Color)
                _Foreground = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>Background</summary>
        ''' <value>Color</value>
        ''' <returns>Background Colour</returns>
        Public Property Background() As Color
            Get
                Return DeserialiseColour(_Background)
            End Get
            Set(ByVal Value As Color)
                _Background = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>Border</summary>
        ''' <value>Color</value>
        ''' <returns>Border Colour</returns>
        Public Property Border() As Color
            Get
                Return DeserialiseColour(_Border)
            End Get
            Set(ByVal Value As Color)
                _Border = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>SectionForeground</summary>
        ''' <value>Color</value>
        ''' <returns>SectionForeground Colour</returns>
        Public Property SectionForeground() As Color
            Get
                Return DeserialiseColour(_SectionForeground)
            End Get
            Set(ByVal Value As Color)
                _SectionForeground = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>SectionBackground</summary>
        ''' <value>Color</value>
        ''' <returns>SectionBackground Colour</returns>
        Public Property SectionBackground() As Color
            Get
                Return DeserialiseColour(_SectionBackground)
            End Get
            Set(ByVal Value As Color)
                _SectionBackground = SerialiseColour(Value)
            End Set
        End Property

        ''' <summary>ItemForeground</summary>
        ''' <value>Color</value>
        ''' <returns>ItemForeground Colour</returns>
        Public Property ItemForeground() As Color
            Get
                Return DeserialiseColour(_ItemForeground)
            End Get
            Set(ByVal Value As Color)
                _ItemForeground = SerialiseColour(Value)
            End Set
        End Property

    End Class
End Namespace
